// pages/feedBack/feedBack.js

const { request } = require('../../API/request');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectData: [
      { id: 1, text: '程序bug' },
      { id: 2, text: '内容意见' },
      { id: 3, text: '网络问题' },
      { id: 4, text: '功能建议' },
      { id: 5, text: '其他' }
    ],
    maxlength: 200,
    radio: 1,
    value: '',
    tempFilePaths: [],
    phoneNum: '',
    lengthErr: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '意见反馈'
    });
    wx.setNavigationBarColor({
      backgroundColor: '#58bcff',
      frontColor: '#ffffff',
    })
  },

  checkOne(e) {
    this.setData({
      radio: e.detail
    })
  },

  inputDetail(e) {
    if (e.detail.value.trim().length >= 15) {
      this.setData({
        lengthErr: false
      })
    }
    this.setData({
      value: e.detail.value
    })
  },

  chooseImage() {
    const that = this;
    let tempFilePaths = this.data.tempFilePaths;
    wx.chooseImage({
      count: 3,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (result) => {
        console.log('图片', result)
        that.setData({
          tempFilePaths: tempFilePaths.concat(result.tempFilePaths)
        })
      }
    })
  },

  previewImage(e) {
    console.log(e);
    wx.previewImage({
      urls: this.data.tempFilePaths,
      current: this.data.tempFilePaths[e.currentTarget.dataset.index]
    })
  },

  deleteImage(e) {
    let tempFilePaths = this.data.tempFilePaths;
    tempFilePaths.splice(e.currentTarget.dataset.index, 1);
    this.setData({
      tempFilePaths
    })
  },
  inputNum(e) {
    console.log(e);
    this.setData({
      phoneNum: e.detail.value
    })
  },

  async submit() {
    const tempFilePaths = this.data.tempFilePaths;
    const describe = this.data.value;
    const phone = this.data.phoneNum;
    const radio = this.data.radio;
    if (describe.trim().length < 15) {
      this.setData({
        lengthErr: true
      })
      return;
    }
    wx.showLoading({
      title: '正在提交',
      mask: true
    });
    // const fs = wx.getFileSystemManager();
    // let ff = [];
    // tempFilePaths.map(v => {
    //   try {
    //     fs.readFile({
    //       filePath: v,
    //       encoding: 'binary',
    //       success(re) {
    //         console.log('图片读取', re);
    //         ff.push(re.data);
    //       }
    //     }) 
    //   } catch (error) {

    //   }

    // })

    // wx.request({
    //   url: 'https://api.xiaoyiiot.cn/feedback/save',
    //   method: 'post',
    //   header: {
    //     'content-type': 'application/x-www-form-urlencoded',
    //     'Authorization': wx.getStorageSync('token') ? 'Bearer ' + wx.getStorageSync('token') : ''
    //   },
    //   data: {
    //     mobile: phone,
    //     files: tempFilePaths,
    //     content: describe,
    //     feedbackType: this.data.selectData[radio - 1].text
    //   },
    //   success(res) {
    //     console.log('意见反馈', res);
    //     if (res.data.code === 200) {
    //       wx.showToast({
    //         title: '提交成功',
    //         duration: 1000,
    //         icon: 'success',
    //         mask: true,
    //       })
    //       // wx.switchTab({
    //       //   url: '/pages/my/my',
    //       // })
    //     }
    //   },
    //   complete(){
    //     wx.hideLoading()
    //   }
    // });
    wx.uploadFile({
      filePath: tempFilePaths[0],
      name: 'files',
      url: 'https://api.xiaoyiiot.cn/feedback/save',
      header:{
        'Authorization': wx.getStorageSync('token') ? 'Bearer ' + wx.getStorageSync('token') : '',
        "content-type":"multipart/form-data"
      },
      formData:{
        mobile: phone,
        content: describe,
        feedbackType: this.data.selectData[radio - 1].text
      },
      success(rr){
        console.log('rr',rr);
      },
      fail(err){
        console.log(err);
      }
    })




  }
})